<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  	<meta charset="${_response_encoding}"/>
    <link rel="stylesheet" type="text/css" href="@{'/public/stylesheets/grid/flexigrid.pack.css'}"/>
	<link rel="stylesheet" type="text/css" href="@{'/public/stylesheets/alert.css'}" />
	<script type="text/javascript" src="@{'/public/javascripts/jquery/jquery-1.4.1.min.js'}"></script>
	<script type="text/javascript" src="@{'/public/javascripts/adu.alert.js'}"></script>
	<script type="text/javascript" src="@{'/public/javascripts/grid/flexigrid.js'}"></script>
	<script type="text/javascript" src="@{'/public/javascripts/jquery_form.js'}"></script>
    <title>图书列表</title>
  </head>
  <body>
  	<div>
  		<div></div>
   		<table id="grid_book" style="display:none"></table>
    </div>
    <script type="text/javascript">
		$(document).ready(function(){
			//table初始化
			var grid=$("#grid_book").flexigrid({
				width: 'auto',//表格宽度
				height: 300,//表格高度
	    		url: '/controllers/BookTypeaction.table',//数据请求地址
	    		dataType: 'json',//请求数据的格式
	    		colModel : [//表格的题头与索要填充的内容。
	    				{display: '分类编号', name : 'id', width : 150, sortable : true, align: 'center',toggle : false},
	    				{display: '所属分类', name : 'fid', width : 80, sortable : true, align: 'center'},
	    				{display: '分类名称', name : 'botName', width : 80, sortable : true, align: 'center'},
	    				{display: '分类代号', name : 'botCode', width : 80, sortable : true, align: 'center'}
	    			],
	    		buttons : [//表格上面的按钮
	    			{name: '新增', displayname: 'add', bclass: 'add' ,onpress: toolbarItem},
	    			{separator: true},
	    			{name: '修改', displayname: 'modify', bclass: 'modify',onpress: toolbarItem},
	                {separator: true},
	    			{name: '删除', displayname: 'delete', bclass: 'delete',onpress: toolbarItem},
	                {separator: true}
    			],
	    		searchitems : [
	    			{display: '书名', name : 'booName'}
    			],
	    		errormsg: '发生异常',
	    		sortname: "id",
	    		sortorder: "desc",
	    		usepager: true,
	    		title: '图书分类管理',
	    		pagestat: '显示记录从{from}到{to}，总数 {total} 条',//分页显示信息
	    		useRp: true,
	    		rp: 10,//默认分页条数
	    		rpOptions: [10,20,50,100,200], //可选择设定的每页结果数
	    		nomsg: '没有符合条件的记录存在',
	    		minColToggle: 1, //允许显示的最小列数
	    		showTableToggleBtn: true,
	    		autoload: true, //自动加载，即第一次发起ajax请求
	    		resizable: true, //table是否可伸缩
	    		procmsg: '加载中, 请稍等 ...',
	    		hideOnSubmit: true, //是否在回调时显示遮盖
	    		blockOpacity: 0.5,//透明度设置
	    		checkbox: true//是否显示第一列的checkbox（用于全选）
	    		
	    	});
	    	
			function toolbarItem(com, grid) {
				switch(com){
					case '删除':
						var selD = $(".trSelected");
						var length = selD.length;
						if(length <= 0){
							aduAlert("请选择至少一条数据!","提示");
						}else{
							var ids_o = selD.find("td[abbr='id']");
							var ids="";
							ids_o.each(function(e){
								ids += "&ids="+$(this).text();
							});
							aduConfirm("确认删除吗?","确认",function(e){
								if(!e)return;
								$.ajax({
									url:'/controllers/BookTypeaction.delS',
									data:ids,
									type:'post',
									success:function(es){
										aduAlert(es,"提示",function(){
											$("#grid_book").flexReload();
										});
									}
								})
							});
						}
						break;
					case '新增':
						var $div = "";
						$.ajax({type:'post',url:'/controllers/BookTypeaction.add',dataType:'html',success:function(e){
							$div="<div>"+e+"</div>"
							aduBox($div,"<font color='red'>图书分类添加</font>");
							$("#add_book_sub").click(function(){
								$("#add_book_type").ajaxSubmit(function(ed){
									if(ed == "0"){
										window.location = "";
									}else{
										if(ed == "1"){
											aduAlert("添加成功!","提示",function(){
												$("#grid_book").flexReload();
											});
										}else{
											aduAlert("添加失败!","提示",function(){});
										}
									}
								}); 
							}); 
							$('#add_book_type').ajaxForm(function() { 
				                return false;
				            }); 
				            select(document.getElementById("findSel"));
						}});
						break;
					case '修改':
						var selD = $(".trSelected");
						var length = selD.length;
						if(length <= 0){
							aduAlert("请选择需要修改的数据!","提示");
						}else{
							var ids_o = selD.find("td[abbr='id']");
							if(length > 1){
								aduAlert("一次只能修改一条数据!","提示",function(){});
							}else{
								$.ajax({
									type:'post',
									url:'/controllers/BookTypeaction.show',
									data:'id='+ids_o.text(),
									dataType:'html',
									success:function(e){
										$div="<div>"+e+"</div>"
										aduBox($div,"<font color='red'>图书分类修改</font>");
										$("#edit_book_sub").click(function(){
											$("#edit_book_type").ajaxSubmit(function(ed){
												if(ed == "0"){
													window.location = "";
												}else{
													if(ed == "1"){
														aduAlert("修改成功!","提示",function(){
															$("#grid_book").flexReload();
														});
													}else{
														aduAlert("修改失败!","提示",function(){});
													}
												}
											}); 
										}); 
										$('#edit_book_type').ajaxForm(function() { 
							                return false;
							            }); 
									}
								});
							}
						}
						break;
				}
        	}
		});
	</script>
	<script type="text/javascript">
		function select(s){
			var o = $(s);
			var os = o.attr("sid");
			var val = o.val();
			if(o.val() != ""){
				$("#"+os).css("display","");
				$.ajax({
					url:'/controllers/BookTypeaction.ajaxBookType',
					type:'post',
					data:'botId='+o.val(),
					dataType:'json',
					success:function(j){
						var i = Number(os.substr(11,os.length));
						var str = 	'<select sid="bookType'+(i+1)+'" onchange="select(this)">';
							str += 	'<option value="" selected="selected">--请选择--</option>';
						var jf = 0 ;
						$.each(j,function(id,e){
							str += 	'<option value="'+e.botId+'">'+e.botName+'</option>';
							jf ++;
						});
							str += 	'</select>';
							str += 	'<span id="bookType'+(i+1)+'"></span>';
						$("#bookType"+i).html(str);
						
						if(jf <= 0){
							$("select[sid='"+'bookType'+(i+1)+"']").css("display","none");
						}
					}
				});
				val = o.val();
			}else{
				var fs = o.parent().attr("id");
				if(fs != null && fs != ""){
					var fo = $("select[sid='"+fs+"']");
					val = fo.val();
				}
				$("#"+os).css("display","none");
			}
			$("#bookInp").val(val);
		}
	</script>
  </body>
</html>